'use strict';


import Base from './base.js';

const querystring = require('querystring');
var ttutil = require("../../business/ttutil");
var http = require('http');
const getToken = require('../../business/getwebtoken.js');
const getUserInfo = require('../../business/getwebuserinfo.js');

export default class extends Base {

    
    async indexAction() {
		var id=await this.param("state");
		//console.log("--------------"+id);
		
            var that=this;
        try
        {
        //微信登录回调
        getToken(this.param("code")).then(function (data) {
			//id=this.param("id");
				//console.log("ididididid=========="+id);
                //console.log(JSON.parse(data));
            return JSON.parse(data);
            
            }).then(function (data) {
            //console.log("==========="+data['access_token']);
			//console.log("access_token======"+data['access_token']);
            //console.log("openid======"+data['openid']);
            getUserInfo(data['access_token'], data['openid']).then(  function (userinfo){
                //{ openid: 'oFZTB1b1TC0ud1zTnX7KOtE6RNpY',
                //nickname: '...',
                //sex: 1,
                //language: 'zh_CN',
                //city: '广州',
                //province: '广东',
                //country: '中国',
                //headimgurl: 'http://thirdwx.qlogo.cn/mmopen/vi_32/zWXSkANEsFM3ywJ1dnIQwtSgXuicwnne21sicbSkxaamj76kEamGBIsvK3F3D1A1ZYFynOzeYHBupnZVJ3Utwm7Q/132',
                //privilege: [],
                //unioonid: 'omQhcwkp-EpLtSwLUv-4XPjOgQe0' }
                //var arr= JSON.stringify(userinfo);  
                //console.log(arr);
                userinfo= JSON.parse(userinfo);
                var openid=userinfo['openid'];
                var unionid=userinfo['unionid'];
                var nickname=userinfo['nickname'];

                //console.log("-------------"+id);

//向辅助服务发送此链接点击事件
			//console.log("id====="+that.param("id"));
			//console.log(unionid+"aaaaaaaaaa"+id);
			if(id!=null&&unionid!=null){
				//console.log(unionid+"aaaaaaaaaa"+id);
            var data = {
                orderID: id,
                ip: ttutil.getClientIP(that.http.req),     
                unionID: unionid
            };
            //http://www.duanxin10086.com/sms.aspx?action=send&userid=12802&account=ydqpyxzx&password=123456&mobile=18050273924&content=123333
        
            data = querystring.stringify(data);
            console.log(data);
        
            http.get('http://47.105.56.254:16923/wxshare?' + data, (res) => {
                 //console.log(`Got response: ${res.statusCode}`);
             //   // consume response body
                console.log("已发送数据");
            }).on('error', (e) => {
                console.log("向服务端发送信息发生错误  "+e);
               
            });
        }
			
                });
            })

            }
            catch(err){return this.display("");}
  

            
        
            this.display("");
        
    }
}